{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%%\n",
    "import pprint\n",
    "import numpy as np \n",
    "import pandas as pd \n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from sklearn.neural_network import MLPRegressor\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.ensemble import GradientBoostingRegressor\n",
    "from sklearn.ensemble import AdaBoostRegressor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%% hyperparameter\n",
    "TRAIN_SIZE = 9000\n",
    "# UpdateTime is an time string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%% get tain_data and test_data\n",
    "data = pd.read_csv(\"sample10000.csv\").drop(labels=\"UpdateTime\",axis=1).to_numpy()\n",
    "labels = pd.read_csv(\"labels10000.csv\").to_numpy()[:,0]\n",
    "\n",
    "train_data = data[:TRAIN_SIZE]\n",
    "test_data = data[TRAIN_SIZE:]\n",
    "\n",
    "train_labels = labels[:TRAIN_SIZE]\n",
    "test_labels = labels[TRAIN_SIZE:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def model_score(model):\n",
    "    #%% predict and score\n",
    "    err = (test_labels -  model.predict(test_data))\n",
    "    sqerr = (err*err).sum()\n",
    "    mse = sqerr*1.0/len(err)\n",
    "    # print(regr.get_params())\n",
    "    #print(\"OOB score\",regr.oob_score_)\n",
    "    print(\"R^2 for train data :\",model.score(train_data,train_labels))\n",
    "    print(\"R^2 for test data :\",model.score(test_data, test_labels))\n",
    "    print(\"mean squre error:\",mse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%% define model and train\n",
    "def test_RF():\n",
    "    regr = RandomForestRegressor(n_estimators=200,oob_score=True)\n",
    "    regr.fit(train_data,train_labels)\n",
    "    model_score(regr)\n",
    "    # R^2 for train data : 0.8658618241928562\n",
    "    # R^2 for test data : -0.07898389513463266\n",
    "    # squre error: 313.67217500000004"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "#%% define DNN(MLPregressor)\n",
    "# R^2 for train data : -2.4562869375355234e-05\n",
    "# R^2 for test data : -0.0005819326087599386\n",
    "# squre error: 290.8798847529891\n",
    "def test_MLP():\n",
    "    mlpr = MLPRegressor(hidden_layer_sizes=(10),\n",
    "                        activation=\"logistic\",batch_size=1000)\n",
    "    # 激活函数很关键，relu 和 identity 基本不工作\n",
    "    # batch_size 影响也很大\n",
    "    mlpr.fit(train_data,train_labels)\n",
    "    model_score(mlpr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_MLP():\n",
    "    mlp_params = {\n",
    "        \"hidden_layer_sizes\": [(x,) for x in range(10,150,10)],\n",
    "        \"activation\":[\"logistic\"],\n",
    "        \"batch_size\":[1000,],\n",
    "        \"learning_rate\":[\"adaptive\"]\n",
    "    }\n",
    "    mlpr = MLPRegressor()\n",
    "    clf = GridSearchCV(mlpr,mlp_params,cv=10)\n",
    "    clf.fit(data,labels)\n",
    "    pprint.pprint(clf.cv_results_)\n",
    "    pprint.pprint(clf.best_params_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "#     R^2 for train data : 0.31327344425793446\n",
    "#     R^2 for test data : 0.04436323074152071\n",
    "#     squre error: 277.81384437241996\n",
    "\n",
    "def test_GBR():\n",
    "    gbr = GradientBoostingRegressor()\n",
    "    gbr.fit(train_data,train_labels)\n",
    "    model_score(gbr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# R^2 for train data : 0.0993108628821433\n",
    "# R^2 for test data : 0.08070103797930306\n",
    "# squre error: 267.2500546046555\n",
    "def test_ABR():\n",
    "    abr = AdaBoostRegressor(learning_rate=0.01)\n",
    "    abr.fit(train_data,train_labels)\n",
    "    model_score(abr)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R^2 for train data : 0.3132734442579346\n",
      "R^2 for test data : 0.0443632307415206\n",
      "mean squre error: 0.27809193630872875\n"
     ]
    }
   ],
   "source": [
    "test_GBR()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "R^2 for train data : 0.0993108628821433\n",
      "R^2 for test data : 0.08070103797930306\n",
      "squre error: 267.2500546046555\n"
     ]
    }
   ],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
